//
//  TestMKUIElementViewSlider.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/7/3.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestMKUIElementViewSlider.h"

@interface TestMKUIElementViewSlider_UISlider : UISlider
@end
@implementation TestMKUIElementViewSlider_UISlider
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [super sizeThatFits:size];
    s.width = MIN(size.width,100);
    return s;
}
@end

@implementation TestMKUIElementViewSlider
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.sliderView = [[TestMKUIElementViewSlider_UISlider alloc] init];
        [self.sliderView addTarget:self action:@selector(_onSliderValueChange:) forControlEvents:(UIControlEventValueChanged)];
        self.accessView = self.sliderView;
    }
    return self;
}
- (void)_updateDetailLabel{
    self.detailText = [NSString stringWithFormat:@"%.2f",self.value];
}
- (void)_onSliderValueChange:(id)sender{
    [self _updateDetailLabel];
    if(self.whenChange){
        self.whenChange(self, self.value);
    }
}
- (void)setValue:(CGFloat)value{
    self.sliderView.value = value;
    [self _updateDetailLabel];
}
- (CGFloat)value{
    return self.sliderView.value;
}
- (CGFloat)maximumValue{
    return self.sliderView.maximumValue;
}
- (void)setMaximumValue:(CGFloat)maximumValue{
    self.sliderView.maximumValue = maximumValue;
}
- (CGFloat)minimumValue{
    return self.sliderView.minimumValue;
}
- (void)setMinimumValue:(CGFloat)minimumValue{
    self.sliderView.minimumValue = minimumValue;
}
@end

@implementation TestMKUIElementViewSlider(Test)
+ (TestMKUIElementViewSlider *)testSliderElementWithTitle:(NSString *)title value:(CGFloat)value minimumValue:(CGFloat)minimumValue maximumValue:(CGFloat)maximumValue whenChange:(void(^)(TestMKUIElementViewSlider *el,CGFloat value))block{
    TestMKUIElementViewSlider *el = [[TestMKUIElementViewSlider alloc] init];
    el.titleText = title;
    el.minimumValue = minimumValue;
    el.maximumValue = maximumValue;
    el.value = value;
    el.whenChange = block;
    return el;
}
@end
